package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassNameSetMealController
 * @Description TODO
 * @Author钰玟
 * @Date2025/2/14 下午12:15
 * @Version 1.0
 **/
@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐管理相关接口")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;


    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO 套餐查询条件对象
     * @return  分页查询结果
     */
    @GetMapping("/page")
    @ApiOperation( "套餐分页查询")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分页查询,{}", setmealPageQueryDTO);
        PageResult pageResult = setMealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     * @param setmealDTO 套餐对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody  SetmealDTO setmealDTO){
        log.info("新增套餐,{}",setmealDTO);
        setMealService.save(setmealDTO);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id  套餐id
     * @return 套餐对象
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealDTO> getById(@PathVariable("id") Long id){
        log.info("根据id查询套餐,{}", id);
        SetmealDTO setmealDTO = setMealService.getById(id);
        return Result.success(setmealDTO);
    }

    /**
     * 修改套餐
     * @param setmealDTO 套餐对象
     * @return 修改结果
     */

    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐,{}",setmealDTO);
        setMealService.updateSetMeal(setmealDTO);
        return Result.success();
    }

    /**
     * 修改套餐状态
     * @param status 套餐状态
     * @param id 套餐id
     * @return 修改结果
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改套餐状态")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateStatus(@PathVariable Integer status, @RequestParam Long id){
        log.info("修改套餐状态,{}",status);
        setMealService.updateStatus(id,status);
        return Result.success();
    }
    /**
     * 批量删除套餐
     * @param ids 套餐id集合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        log.info("删除套餐,{}",ids);
        setMealService.deleteBatch(ids);
        return Result.success();
    }


}
